package uk.ac.ebi.uniprot.wikipedia.util;

import org.apache.log4j.Logger;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * User: ljgarcia
 * Date: 11/06/12
 * Time: 15:58
 * To change this template use File | Settings | File Templates.
 */
public class RetrievalHelper {
    private static Logger logger = Logger.getLogger(RetrievalHelper.class);
    private static Properties queries = null;
    private static final String QUERY_PROPERTIES_FILE = "/query.properties";

    private static void initPropertiesFile() {
        if (queries == null) {
            try {
                queries = new Properties();
                queries.load(new FileInputStream(QUERY_PROPERTIES_FILE));
            } catch (Exception e) {
                logger.info("Properties file could not be read, default properties will be used. ERROR: " + e.getMessage());
                queries = new Properties();
                queries.put("GENE_WIKI", "SELECT DISTINCT ?s str(?entrez) AS ?entrezId str(?acc) AS ?upAcc WHERE{?s a gw_wiki:Category-3AHuman_proteins; gw_property:{0} ?entrez. OPTIONAL{?s gw_property:{1} ?acc} MINUS {?s swivt:redirectsTo ?redirects}}");
                queries.put("GENE_WIKI_TEST_HUMAN_WITH", "SELECT DISTINCT ?s str(?entrez) AS ?entrezId str(?acc) AS ?upAcc WHERE{?s a gw_wiki:Category-3AHuman_proteins; gw_property:{0} ?entrez. OPTIONAL{?s gw_property:{1} ?acc}. FILTER(?s = <http://genewikiplus.org/wiki/Special:URIResolver/DDX20>) MINUS {?s swivt:redirectsTo ?redirects}}");
                //queries.put("GENE_WIKI_TEST_HUMAN_WITH", "SELECT DISTINCT ?s str(?entrez) AS ?entrezId str(?acc) AS ?upAcc WHERE{?s a gw_wiki:Category-3AHuman_proteins; gw_property:{0} ?entrez. OPTIONAL{?s gw_property:{1} ?acc}. FILTER(?s = <http://genewikiplus.org/wiki/Special:URIResolver/Cryptochrome>) MINUS {?s swivt:redirectsTo ?redirects}}");
                queries.put("SPARQL", "http://virtuoso.idiginfo.org/sparql?default-graph-uri=&query={0}&format=application/json&timeout=0&debug=on");
                queries.put("UP_SEARCH_BY_GENE", "http://www.uniprot.org/mapping?from=ACC&to=P_ENTREZGENEID&format=tab&query={0}");
            }
        }
    }

    public static String getProperty(String property) throws IOException {
        // Lazy load properties file
        initPropertiesFile();
        return (queries.getProperty(property));
    }
    /**
     * Utility method to build a URL, based upon the contents of a properties file
     * to define the URLs.
     *
     * @param key       being the name (key) of the required URL
     * @param arguments zero or more String arguments that are placed into the URL.
     * @return a well formed URL.
     * @throws java.io.IOException - if the URL map file is not found
     */
    public static String buildQuery(String key, String... arguments) throws IOException {
        // Lazy load properties file
        initPropertiesFile();
        //logger.debug("key "+key);
        String queryFormat = queries.getProperty(key.toUpperCase());

        //logger.debug("queryFormat "+queryFormat);
        if (queryFormat == null) {
            return null;
        } else {
            for (int i=0; i < arguments.length; i++) {
                queryFormat = queryFormat.replace("{" + i + "}", arguments[i]);
            }
            //return MessageFormat.format(queryFormat, arguments);
            return queryFormat;
        }
    }
}
